package com.jiaertangshi.jiaer.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 操作订单的操作类
 * @author 98157
 *
 */
import org.springframework.transaction.annotation.Transactional;

import com.jiaertangshi.jiaer.controller.BillController;
import com.jiaertangshi.jiaer.dao.BusinessMapper;
import com.jiaertangshi.jiaer.dao.ShopMapper;
import com.jiaertangshi.jiaer.dao.TbillMapper;
import com.jiaertangshi.jiaer.dao.TorderMapper;
import com.jiaertangshi.jiaer.dao.TuserMapper;
import com.jiaertangshi.jiaer.dto.Business;
import com.jiaertangshi.jiaer.dto.Tbill;
import com.jiaertangshi.jiaer.dto.Torder;
import com.jiaertangshi.jiaer.dto.Tuser;
import com.jiaertangshi.jiaer.dto.RequestParameter.ToderRequest;
import com.jiaertangshi.jiaer.tools.wechat.JiaResult;

import io.swagger.annotations.ApiOperation;
@Service
public class BillService {
	
	public static final Logger logger = LoggerFactory.getLogger(BillController.class);
	
	//这个是订单表
	@Autowired
	private TbillMapper tbillMapper; 
	
	//订单详情表
	@Autowired
	private TorderMapper toderMapper;

	//交易记录表
	@Autowired
	private BusinessMapper businessMapper;
	
	//用户表
	@Autowired
	private TuserMapper tuserMapper;
	
	//商户表
	@Autowired
	private ShopMapper shopMapper;
	
	/**
	 * 微信支付使用的service层方法
	 * 操作记录订单表和插入订单详情表
	 * 交易流水账号
	 * @param tbill
	 * @param todlist
	 * @return
	 */
	@Transactional
	public void addOrader(Tbill tbill,List<Torder> Toders) {
		tbill.setBillState("0");//未付款
		tbill.setDataStatus(true);//默认是有效的状态
		tbill.setCreatedAt(new Date());//订单创建时间
		tbill.setUpdatedAt(new Date());//订单更新时间
		/**
		 * 插入订单数据
		 */
		tbillMapper.insert(tbill);
		//插入订单详情数据
		toderMapper.insertOrderListBatch(tbill.getId(),tbill.getUserid(),tbill.getShopid(),Toders);
		/**
		 * 插入交易流水
		 */
		Business bus  = new Business();
		bus.setTransactionType("收入");
		bus.setTransactionRes("点餐");
		bus.setTransactionValue(tbill.getConsumeAmount());
		bus.setTransactionFun("微信");
		bus.setTransactionInno(null);//交易收入账户
		bus.setTransactionOutno(null);//交易支出账户
		bus.setTransactionInuserid(tbill.getShopid());//交易收入用户Id
		bus.setTransactionOutuserid(tbill.getUserid());//交易支出账号
		bus.setTransactionReno(tbill.getBillNo());//交易流水号
		bus.setTransactionRealuserid(tbill.getUserid());//处理人
		bus.setTransactionBillid(tbill.getId());//订单Id
		bus.setTransactionDate(new Date());//交易时间
		bus.setDataStatus(false);//数据是否有效
		bus.setCreatedAt(new Date());//创建时间
		bus.setUpdatedAt(new Date());//更新时间
		businessMapper.insert(bus); //创建订单流水账号
	}
	
	/**
	 * 
	 * @param out_trade_no  用户订单
	 * @return 查询到的订单对象
	 */
	public Tbill geTbillByNo(String out_trade_no) {
		return 	tbillMapper.selectByNo(out_trade_no);
	}
	
	/**
	 * 更新订单信息
	 * @param tbill
	 */
	public void updateBill(Tbill tbill) {
		tbillMapper.updateByPrimaryKey(tbill);
		Business billId = businessMapper.selectByBillId(tbill.getBillNo(),tbill.getId());
		if(!billId.getDataStatus()) {
			billId.setDataStatus(true);
			billId.setTransactionDate(new Date()); //交易时间
			billId.setUpdatedAt(new Date()); //更新时间
		}
		businessMapper.updateByPrimaryKey(billId);
	}
	
	@ApiOperation(value="服务层根据用户Id获取到用户订单",notes="获取用户订单")
	public JiaResult getBillByUserId(Integer userId,Integer page,Integer size) {
		//这里获取到开始的个数
		int start = (page -1) * size;
		List<Tbill> list = tbillMapper.selectByUser(start, size, userId);
		return JiaResult.ok(list);
	}
	/**
	 * 订单生成
	 * 余额支付
	 * @return
	 */
	@Transactional
	public JiaResult addTbillBalance(Tbill tbill,ToderRequest tods) {
		//这里获取到用户的全部信息
		Tuser tuser = tuserMapper.selectByPrimaryKey(tbill.getUserid()); //这里获取到User对象
		//判断用户的余额和奖金是否够
		if(tuser.getUserBalance().compareTo(tbill.getPayAmount()) >= 0) {
		    /**
		     * 修改用户余额表
		     */
			tuser.setUserBalance(tuser.getUserBalance().subtract(tbill.getPayAmount())); //用户余额
			tuser.setUpdatedAt(new Date()); //更新时间
			/**
			 * 创建订单数据
			 */
			tbill.setBillState("1");//已付款
			tbill.setDataStatus(true);//默认是有效的状态
			tbill.setPayDate(new Date()); //付款时间
			tbill.setCreatedAt(new Date());//订单创建时间
			tbill.setUpdatedAt(new Date());//订单更新时间	
			//更新用户余额
			tuserMapper.updateByPrimaryKey(tuser);
			//更新商户余额
			shopMapper.updateShopbalance(tbill.getPayAmount(),tbill.getShopid());
			//插入订单数据
			tbillMapper.insert(tbill);
			//这里判断点餐模式是单点模式
			if(tbill.getBillPatn().equals("单点")) {
				//插入订单详情数据
				toderMapper.insertOrderListBatch(tbill.getId(),tbill.getUserid(),tbill.getShopid(),tods.getToders());
			}
			/**
			 * 创建交易流程表数据
			 */
			Business bus  = new Business();
			bus.setTransactionType("收入");
			bus.setTransactionRes("点餐");
			bus.setTransactionValue(tbill.getConsumeAmount());
			bus.setTransactionFun("用户余额");
			bus.setTransactionInno(null);//交易收入账户
			bus.setTransactionOutno(null);//交易支出账户
			bus.setTransactionInuserid(tbill.getShopid());//交易收入用户Id
			bus.setTransactionOutuserid(tbill.getUserid());//交易支出账号
			bus.setTransactionReno(tbill.getBillNo());//交易流水号
			bus.setTransactionRealuserid(tbill.getUserid());//处理人
			bus.setTransactionBillid(tbill.getId());//订单Id
			bus.setTransactionDate(new Date());//交易时间
			bus.setDataStatus(true);//数据是否有效
			bus.setCreatedAt(new Date());//创建时间
			bus.setUpdatedAt(new Date());//更新时间
			//插入交易流水
			businessMapper.insert(bus); //创建订单流水账号
			
			return JiaResult.build(200,"交易成功!");
		}else {
			return JiaResult.build(500,"用户余额不足！");
		}
	}
	/**
	 * 订单生成
	 * 奖金支付
	 */
	@Transactional
	public JiaResult addTbillBonus(Tbill tbill,ToderRequest tods) {
		//这里获取到用户的全部信息
		Tuser tuser = tuserMapper.selectByPrimaryKey(tbill.getUserid()); //这里获取到User对象
		//判断用户的奖金是否够
		if(tuser.getUserBonus().compareTo(tbill.getPayAmount()) >= 0) {
		    /**
		     * 修改用户奖金
		     */
			tuser.setUserBonus(tuser.getUserBonus().subtract(tbill.getPayAmount())); //用户奖金
			tuser.setUpdatedAt(new Date()); //更新时间
			/**
			 * 创建订单数据
			 */
			tbill.setBillState("1");//已付款
			tbill.setDataStatus(true);//默认是有效的状态
			tbill.setPayDate(new Date()); //付款时间
			tbill.setCreatedAt(new Date());//订单创建时间
			tbill.setUpdatedAt(new Date());//订单更新时间	
			//更新用户余额
			tuserMapper.updateByPrimaryKey(tuser);
			//更新商户余额
			shopMapper.updateShopbalance(tbill.getPayAmount(),tbill.getShopid());
			//插入订单数据
			tbillMapper.insert(tbill);
			if(tbill.getBillPatn().equals("单点")) {
				//插入订单详情数据
				toderMapper.insertOrderListBatch(tbill.getId(),tbill.getUserid(),tbill.getShopid(),tods.getToders());
			}
			/**
			 * 创建交易流程表数据
			 */
			Business bus  = new Business();
			bus.setTransactionType("收入");
			bus.setTransactionRes("点餐");
			bus.setTransactionValue(tbill.getConsumeAmount());
			bus.setTransactionFun("用户奖金");
			bus.setTransactionInno(null);//交易收入账户
			bus.setTransactionOutno(null);//交易支出账户
			bus.setTransactionInuserid(tbill.getShopid());//交易收入用户Id
			bus.setTransactionOutuserid(tbill.getUserid());//交易支出账号
			bus.setTransactionReno(tbill.getBillNo());//交易流水号
			bus.setTransactionRealuserid(tbill.getUserid());//处理人
			bus.setTransactionBillid(tbill.getId());//订单Id
			bus.setTransactionDate(new Date());//交易时间
			bus.setDataStatus(true);//数据是否有效
			bus.setCreatedAt(new Date());//创建时间
			bus.setUpdatedAt(new Date());//更新时间
			//插入交易流水
			businessMapper.insert(bus); //创建订单流水账号
			return JiaResult.build(200,"交易成功!");
		}else {
			return JiaResult.build(500,"用户奖金不足！");
		}
	}
}
